package ruifeng.zcj.communcationring.ui.reward;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;

import huanxin.ui.VideoCallActivity;
import huanxin.ui.VoiceCallActivity;
import ruifeng.zcj.communcationring.R;
import ruifeng.zcj.communcationring.base.App;
import ruifeng.zcj.communcationring.base.base_web_activity.WebViewActivity;
import ruifeng.zcj.communcationring.base.base_web_activity.other.JS;
import ruifeng.zcj.communcationring.groble.FragmentArgs;
import ruifeng.zcj.communcationring.ui.circle.other.CircleJS;
import ruifeng.zcj.communcationring.ui.reward.bean.Video;

/**
 * 悬赏
 * Created by Administrator on 2016/9/10 0010.
 */
public class RewardActivity extends WebViewActivity {

    public static void launch(Activity activity,
                              Class<? extends Fragment> clazz,
                              FragmentArgs args) {
        Intent intent = new Intent(activity, RewardActivity.class);
        intent.putExtra(KEY_CLASS_NAME, clazz.getName());
        if (args != null) {
            intent.putExtra(KEY_ARGS, args);
        }
        activity.startActivity(intent);
    }

    @Override
    public void initView() {
        String className = getIntent().getStringExtra(KEY_CLASS_NAME);
        if (TextUtils.isEmpty(className)) {
            finish();
            return;
        }
        FragmentArgs values = (FragmentArgs) getIntent().getSerializableExtra(KEY_ARGS);
        MyURL = (String) values.get("CircleFragment");

        super.initView();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        mWebview = view;
        return true;
    }


    // 是否调用js代码默认是传递
    @Override
    protected boolean isAddJavascriptInterface() {
        return true;
    }

    // new一个子类
    @Override
    public JS newJsObj() {// 传递一个子类给
        CircleJS aa = new CircleJS();
        aa.jsona = "zcj";
        return aa;
    }

    // 一个key
    @Override
    public String HTMLkey() {// 获取他的key
        return "Android";
    }

    Video mVideo = null;

    // js代码调用java代码之后该做什么事情做什么事情
    public void pay(String arg) {
        if (!TextUtils.isEmpty(arg)) {
            mVideo = Video.parasjson(arg);
            if (mVideo != null) {
                String aa  = mVideo.getUsername().toString();
                if (mVideo.getMethod().equals("v")) {// 视频
                    RewardActivity.this.startVideoCall(aa);// mVideo.getUsername()
                } else if (mVideo.getMethod().equals("a")) {// 语音
                    RewardActivity.this.startVoiceCall(aa);
                }
            }
        }
    }

    public void finishActivity() {
        RewardActivity.this.finish();
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (mWebview != null) {
            mWebview.removeAllViews();
            mWebview.destroy();
        }
    }

    /****
     * 视频通话
     **/
    public void startVideoCall(String username) {
        if (!EMChatManager.getInstance().isConnected())
            Toast.makeText(App.getAppContext(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
        else {// 拨打视频
            startActivity(new Intent(App.getAppContext(), VideoCallActivity.class).putExtra("username", username)
                    .putExtra("isComingCall", false));
        }
    }

    /**
     * 拨打语音电话
     */
    public void startVoiceCall(String username) {
        if (!EMChatManager.getInstance().isConnected()) {
            Toast.makeText(App.getAppContext(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
        } else {
            startActivity(new Intent(App.getAppContext(), VoiceCallActivity.class).putExtra("username", username)
                    .putExtra("isComingCall", false));

        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
       /* if (mWebview != null) {
            mWebview.loadUrl(MyURL);
        }*/
        finish();
    }
}
